<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑用户信息</title>
    <link rel="stylesheet" type="text/css" href="../layui/css/layui.css">
</head>
<body>
<form class="layui-form" lay-filter="edit"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <div class="layui-input-inline">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名：</label>
            <div class="layui-input-block" >
                <input type="text" name="id"  style="display: none" class="layui-input">
                <input type="text" name="username" placeholder="请输入用户名" autocomplete="off" lay-verify="required" lay-reqText="用户名不能为空" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">昵称：</label>
            <div class="layui-input-block">
                <input type="text" name="nickname" placeholder="请输入昵称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码：</label>
            <div class="layui-input-block">
                <input type="text" name="password" placeholder="请输入密码" autocomplete="off" lay-verify="required" lay-reqText="密码不能为空" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-input-inline" style="margin-left: 30px">
        <div class="layui-upload-drag" id="upImg">
            <i class="layui-icon"></i>
            <p>点击更换头像，或将图片拖拽到此处</p>
            <div class="layui-hide" id="uploadDemoView">
                <hr>
                <input type="text" name="faceImage"  autocomplete="off" placeholder="" class="layui-input layui-hide">
                <img src="" alt="上传成功后渲染" style="max-width: 196px">
            </div>
        </div>
    </div>

    <div class="layui-form-item" >
        <label class="layui-form-label">电话：</label>
        <div class="layui-input-block" style="width: 510px">
            <input type="text" name="phone" placeholder="请输入电话" autocomplete="off" lay-verify="number|phone" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邮箱：</label>
        <div class="layui-input-block" style="width: 510px">
            <input type="text" name="email" placeholder="请输入邮箱" autocomplete="off" lay-verify="email"  class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="sub" id="sub">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script type="text/javascript" src="../layui/layui.js"></script>
<script>
    layui.use(['form','upload'],function(){
        var form = layui.form,
            upload = layui.upload,
            $ = layui.jquery;
        //拖拽上传
        form.render()
        form.val('edit',{
            "id":parent.row.id,
            "username":parent.row.username,
            "nickname":parent.row.nickname,
            "password":parent.row.password,
            "phone":parent.row.phone,
            "email":parent.row.email,


        })
        layui.$('#uploadDemoView').removeClass('layui-hide').find('img').attr("src","../"+parent.row.faceImage);
        upload.render({
            elem: '#upImg'
            ,url: '../user/up' //改成您自己的上传接口
            ,accept: 'images'
            ,done: function(res){
                layer.msg(res.msg)
                if(res.code == 200){

                    layui.$('#uploadDemoView').removeClass('layui-hide').find('input').val("img/"+res.data);
                    layui.$('#uploadDemoView').removeClass('layui-hide').find('img').attr("src","../img/"+res.data);
                }else{
                    layui.$('#uploadDemoView').removeClass('layui-hide').find('input').val(res.msg);
                }
            }

        });

        //监听提交
        form.on('submit(sub)', function(data){
            $.ajax({
                type:"post",
                url:"../user/update",
                dataType:"json",
                contentType:"application/json",
                data:JSON.stringify(data.field),
                success:function(res){
                    //执行成功以后的操作
                    layer.msg(res.msg,function(){
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        parent.layui.table.reload('user');
                        console.log("dddddddddddddd")
                    })
                }
            })

            // 阻止页面跳转--取消submit默认提交机制
            return false;
        });


    })
</script>
</body>
</html>